//
//  PYFileManager.m
//  IHome
//
//  Created by Jim on 2019/10/3.
//  Copyright © 2019 com.zhujizheng. All rights reserved.
//

#import "PYFileManager.h"
@interface PYFileManager ()
@property (nonatomic, strong) NSOperationQueue *queue;
@end
@implementation PYFileManager
+ (instancetype)sharedManager
{
    static PYFileManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [PYFileManager new];
    });
    return manager;
}
- (NSOperationQueue *)queue
{
    if (!_queue) {
        _queue = [[NSOperationQueue alloc] init];
        _queue.maxConcurrentOperationCount = 3;
    }
    return _queue;
}

/// 获取Document目录下的路径
+ (nullable NSString *)getPathInDocumentWithSubPath:(nonnull NSString *)subPath
{
    if (!subPath) {
        return nil;
    }
    NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    if (!documentsDir) {
        return nil;
    }
    if (!subPath) {
        return documentsDir;
    }
    NSString *fullPath = [documentsDir stringByAppendingPathComponent:subPath];
    return fullPath;
}

/// 生成Document目录下的文件夹路径
+ (nullable NSString *)getOrCreateDirectoryInDocumentWithSubPath:(nonnull NSString *)subPath
{
    NSString *fullPath = [self getPathInDocumentWithSubPath:subPath];
    if (!fullPath) {
        return nil;
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL existed = [fileManager fileExistsAtPath:fullPath isDirectory:&isDir];
    if (!isDir || !existed) {
        BOOL success = [fileManager createDirectoryAtPath:fullPath
                              withIntermediateDirectories:YES
                                               attributes:nil
                                                    error:nil];
        if (success == NO) {
            NSLog(@"创建文件夹路径出错");
            return nil;
        } else {
            NSLog(@"创建文件夹路径成功");
        }
    } else {
        NSLog(@"该路径是文件夹且已存在");
    }
    return fullPath;
}

/// 删除指定路径的文件
+ (void)deleteByName:(NSString *)name fromDocumentDirectory:(NSString *)directory
{
    NSString *filePathDir = [self getPathInDocumentWithSubPath:directory];
    NSString *filePath = [filePathDir stringByAppendingPathComponent:name];
    [self deleteFileWithPath:filePath];
}
+ (void)deleteFileWithPath:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        BOOL success = [fileManager removeItemAtPath:filePath error:nil];
        if (!success) {
            NSLog(@"删除文件操作失败, filePath = %@", filePath);
        } else {
            NSLog(@"删除文件成功, filePath = %@", filePath);
        }
    } else {
        NSLog(@"删除文件失败, 文件不存在，filePath = %@", filePath);
    }
}
+ (void)deleteAllFilesInDocumentDirectory:(NSString *)directory
{
    NSArray *fileNames = [self getAllFileNamesFromDocumentDirectory:directory];
    for (NSString *fileName in fileNames) {
        [self deleteByName:fileName fromDocumentDirectory:directory];
    }
}
/// 获取指定路径的所有文件名
+ (nullable NSArray<NSString *> *)getAllFileNamesFromDocumentDirectory:(NSString *)directory
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *pathDir = [self getPathInDocumentWithSubPath:directory];
    if ([fileManager fileExistsAtPath:pathDir] == NO) {
        NSLog(@"获取指定路径的所有文件名失败, 文件夹不存在 pathDir = %@", pathDir);
        return nil;
    }
    NSError *error = nil;
    NSArray *files = [fileManager contentsOfDirectoryAtPath:pathDir error:&error];
    if (error) {
        NSLog(@"获取指定路径的所有文件名操作失败, error = %@", error);
    } else {
        NSLog(@"获取指定路径的所有文件名成功，files = %@", files);
    }
    return files;
}
/// 复制文件到目标文件
+ (BOOL)copyFromPath:(NSString *)fromPath
              toPath:(NSString *)toPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    [PYSystemTool beginTest];
    BOOL success = [fileManager copyItemAtPath:fromPath toPath:toPath error:&error];
    [PYSystemTool endTest];
    if (success) {
        NSLog(@"复制文件到目标文件成功, fromPath = %@, toPath = %@", fromPath, toPath);
    } else {
        NSLog(@"复制文件到目标文件操作失败, fromPath = %@, toPath = %@, error = %@", fromPath, toPath, error);
    }
    return success;
}

+ (BOOL)isExistForPath:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:path];
}

+ (unsigned long long)getFileSizeForPath:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float fileSize = 0;
    if ([fileManager fileExistsAtPath:path]) {
        fileSize = [[fileManager attributesOfItemAtPath:path error:nil] fileSize];
    }
    return fileSize;
}
@end
